package com.alstudio.utils.android.geolocation;

import android.content.Context;

import com.alstudio.pojo.module.server.GeographicLocation;
import com.alstudio.utils.android.geolocation.ALGpsManager.AlGpsLocationListener;
import com.alstudio.utils.android.net.ALTelePhoneManager;
import com.alstudio.utils.android.net.ALTelePhoneManager.AlPhoneLocationListener;
import com.alstudio.utils.android.net.ALWifiManager;
import com.alstudio.utils.log.ALLog;

/**
 * 地理位置管理器，监听由GPS、基站、wifi回调的地理位置信息
 * 
 * @author alonso lee
 * 
 */
public class ALLbsManager {

	private static ALLbsManager instance;

	private static Context mContext;

	public static ALLbsManager getInstance(Context context) {

		if (instance == null) {

			if (context == null) {
				throw new IllegalAccessError("context object should not null!");
			}

			mContext = context;
			instance = new ALLbsManager();
		}

		return instance;
	}

	public void startLocating(LbsModuleConfig config) {
		switch (config) {
		case ENABLE_GPS_AND_PHONE_LOCATION:
			ALGpsManager.getInstance(mContext).startGPSLocateModule(
					gpsLocationListener);
			ALTelePhoneManager.startPhoneLocateModule(mContext,
					phoneLocationListener);
			break;
		case ENABLE_GPS_LOCATION_ONLY:
			ALGpsManager.getInstance(mContext).startGPSLocateModule(
					gpsLocationListener);
			break;
		case ENABLE_PHONE_LOCATION_ONLY:
			ALTelePhoneManager.startPhoneLocateModule(mContext,
					phoneLocationListener);
			break;
		case ENABLE_NONE:
			break;
		default:
			break;
		}
	}

	public GeographicLocation getGeographicLocationData() {
		GeographicLocation data = new GeographicLocation();

		data.setBssid(ALWifiManager.getCurrentBssid(mContext));
		data.setCdmaLat(ALTelePhoneManager.getCdmaLat(mContext));
		data.setCdmaLng(ALTelePhoneManager.getCdmaLng(mContext));
		data.setCellid(ALTelePhoneManager.getCellId(mContext));
		data.setLac(ALTelePhoneManager.getLac(mContext));
		data.setLat(ALGpsManager.getInstance(mContext).getLat());
		data.setLng(ALGpsManager.getInstance(mContext).getLng());

		return data;
	}

	public void stopLocateService() {
		ALTelePhoneManager.stopPhoneLocateModule(mContext);
		ALGpsManager.getInstance(mContext).stopGPSLocateModule();
	}

	private AlGpsLocationListener gpsLocationListener = new AlGpsLocationListener() {

		@Override
		public void onLocationChanged(ALGeoLocationData newLocation) {
			// TODO Auto-generated method stub
			// gps位置变化通知
			ALLog.d("gps位置变化回掉通知");
		}
	};

	private AlPhoneLocationListener phoneLocationListener = new AlPhoneLocationListener() {

		@Override
		public void onLocationChanged(ALGeoLocationData newLocation) {
			// TODO Auto-generated method stub
			// 电话位置变化回掉通知
			ALLog.d("电话位置变化回掉通知");
		}
	};

	public enum LbsModuleConfig {
		// 启用gps和基站定位
		ENABLE_GPS_AND_PHONE_LOCATION,
		// 仅启用gps定位
		ENABLE_GPS_LOCATION_ONLY,
		// 仅启用基站定位
		ENABLE_PHONE_LOCATION_ONLY,
		// 不启用定位
		ENABLE_NONE,
	}
}
